package com.zz.tijian.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zz.tijian.entity.*;
import com.zz.tijian.entity.res.*;
import com.zz.tijian.mapper.CireportMapper;
import com.zz.tijian.service.*;
import com.zz.tijian.utils.UserContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 17958
* @description 针对表【cireport】的数据库操作Service实现
* @createDate 2024-06-13 12:05:47
*/
@Service
public class CireportServiceImpl extends ServiceImpl<CireportMapper, Cireport>
    implements CireportService {

    @Autowired
    @Lazy
    private OrdersService ordersService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private OverallresultService overallresultService;
    @Autowired
    private CidetailedreportService cidetailedreportService;
    @Override
    public Result<List<ReportListVo>> getAllReport() {
        //1.获取用户信息
        String userId = UserContext.currentUserId();
        //2.查询该用户已经归档的订单
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,userId).eq(Orders::getState,4);
        List<Orders> list = ordersService.list(queryWrapper);
        List<ReportListVo> reportSVoList=new ArrayList<>();
        for( Orders orders : list){
            ReportListVo reportSVo = new ReportListVo();
            BeanUtils.copyProperties(orders,reportSVo);
            Hospital hp = hospitalService.getById(orders.getHpId());
            reportSVo.setHpName(hp.getName());
            reportSVoList.add(reportSVo);
        }
        return Result.ok(reportSVoList) ;
    }

    @Override
    public Result<ReportVo> getReportDetail(Integer orderId) {
        //0.初始化
        ReportVo reportVo = new ReportVo();
        reportVo.setReportDetails(new ArrayList<>());
        reportVo.setReportOverall(new ReportOverallVo());
        reportVo.getReportOverall().setErrors(new ArrayList<>());
        //1.获取总检
        LambdaQueryWrapper<Overallresult> query=new LambdaQueryWrapper<>();
        query.eq(Overallresult::getOrderId,orderId);
        List<Overallresult> overallList = overallresultService.list(query);
        reportVo.getReportOverall().setOverallResults(overallList);
        //2. 获取体检明细、异常项
        LambdaQueryWrapper<Cireport> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Cireport::getOrderId,orderId);
        List<Cireport> list = this.list(queryWrapper);

        for(Cireport cireport: list){
            LambdaQueryWrapper<Cidetailedreport> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Cidetailedreport::getOrderId,orderId)
                            .eq(Cidetailedreport::getCiId,cireport.getCiId());
            List<Cidetailedreport> details = cidetailedreportService.list(lambdaQueryWrapper);
            //异常项
            for (Cidetailedreport temp :details){
                if(temp.getIsError()==1){
                    reportVo.getReportOverall().getErrors().add(temp);
                }
            }

            //组装
            ReportDetailVo reportDetailVo = new ReportDetailVo();
            reportDetailVo.setCiName(cireport.getCiName());
            reportDetailVo.setCidetailedreports(details);
            //添加
            reportVo.getReportDetails().add(reportDetailVo);
        }
        return Result.ok(reportVo);
    }
}




